package com.trkj.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbpost;
import com.trkj.pojos.Tbrole;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.Tbtitle;
import com.trkj.service.HwcDepartmentService;
import com.trkj.service.HwcStaffService;
import com.trkj.vo.Result;

@Controller
public class HwcController extends SimpleTagSupport {
	
	@Autowired
	private HwcDepartmentService deptService;
	
	@Autowired
	private	HwcStaffService	staffService;
	/**
	 * 注销
	 * 
	 * 
	 */
	@RequestMapping("/to_invalidateSession")
	public String invalidateSession(HttpSession session){
		session.invalidate();
		System.err.println("注销");
		return "hwc_login";
	}
	/**
	 * 登录（控制器）
	 * @param account
	 * @param password 
	 * @return
	 */
	@RequestMapping("/loginTest")
	public ModelAndView login(HttpSession session,String account,String password){
		ModelAndView mv = new ModelAndView();
		if(account!=null&&password!=null){
			boolean pd = staffService.findAccountByStaff(account, password);
			//long staffNo = staffService.staffAccountTOStaffNo(account);
			if(pd == true){
				session.setMaxInactiveInterval(Integer.MAX_VALUE);
				long staffNo =Long.parseLong(account.substring(6));
				String staffName = staffService.findAccountBySName(account);
				session.setAttribute("staffNo", staffNo);
				session.setAttribute("account", account);
				mv.addObject("staffAccount", account);
				mv.addObject("staffPassword", password);
				mv.addObject("staffName",staffName);
				mv.addObject("staffNo",staffNo);
				//return "hwc_loginresult";
				mv.setViewName("his_HIS");
			}else{
				//return "yzr_index";
				mv.setViewName("hwc_loginresult");
			}
			return mv;
			
		}else{
			mv.setViewName("hwc_login");
			return mv;
		}
		
	} 
	
	/**
	 * 登录（控制器）
	 * @param account
	 * @param password
	 * @return
	 */
	@RequestMapping("/hwc_login")
	public ModelAndView loginTest(HttpSession session,@RequestParam String account,@RequestParam String password){
		boolean pd = staffService.findAccountByStaff(account, password);

		ModelAndView mv = new ModelAndView();
		
		if(pd == true){
			long staffNo =Long.parseLong(account.substring(6));
			String staffName = staffService.findAccountBySName(account);
			session.setAttribute("account",account); 
			mv.addObject("staffAccount", account);
			mv.addObject("staffPassword", password);
			mv.addObject("staffName",staffName);
			mv.addObject("staffNo",staffNo);
			
			//mv.setViewName("hwc_index");
			mv.setViewName("his_HIS");
			
		}else{
			
			//return "yzr_index";
			
			mv.setViewName("hwc_loginresult");
			
		}
		
		return mv;
	} 


	/**页面跳转*********************************************************************/
	@RequestMapping("/toLoginResult")
	public String toLoginResult(){
		return "hwc_loginresult";
	}
	/**
	 * 测试
	 */
	@RequestMapping("/toTest")
	public String toTest(){
		return "hwc_test";
	}
	/**
	 * 跳转TestTwo页面
	 * @return
	 */
	@RequestMapping("/toTestTwo")
	public String toTestTwo(){
		return "hwc_test_two";
	}
	/**
	 * 登录
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin(){
		return "hwc_login";
	}
	
	/**
	 * 登录测试
	 * @return
	 */
	@RequestMapping("/toLoginTest")
	public String toLoginTest(){
		return "hwc_login_test";
	}
	
	/**
	 * 跳转科室
	 */
	@RequestMapping("/toDepartment")
	public String toDepartment(){
		return "hwc_department";
	}
	
	/**
	 * 跳转员工页面
	 */
	@RequestMapping("/toStaff")
	public String toStaff(){
		return "hwc_staff";
	}
	
	/**功能****************************************************************************/
	/**
	 * 分页显示科室信息
	 */
	@RequestMapping("/queryByDepartmentPage")
	public @ResponseBody Map<String,Object> queryDepartmentByPage(int page,int rows){
	
		return deptService.queryDepartmentPage(page, rows);
	}
	
	/**
	 * 显示所有科室信息
	 */
	@RequestMapping("/queryAllTbdepartment")
	public @ResponseBody List<Tbdepartment> queryAllDepartment(){
		return deptService.queryAllDepartment();
	}
	/**
	 * 添加科室
	 */
	@RequestMapping("/addNewTbdepartment")
	public @ResponseBody Result addNewTbdepartment(Tbdepartment tbdepartment){	
		deptService.addNewDept(tbdepartment);
		return new Result(true,"添加成功");
	}
	/**
	 * 修改科室信息
	 */
	@RequestMapping("/editDepartment")
	public @ResponseBody Result editDepartment(Tbdepartment tbdepartment){
		deptService.editTbDepartment(tbdepartment);
		return new Result(true,"修改成功");
	}
	
	/**
	 * 根据ID查找科 室
	 */                          
	@RequestMapping("/findTbdepartmentById")
	public @ResponseBody Tbdepartment deleteDepartment(long departmentno){

		Tbdepartment tbdepartment =deptService.findTbdepartmentById(departmentno);

		return tbdepartment;
	}
	
	/**
	 * 删除科室
	 */
	@RequestMapping("/deleteTbdepartment")
	public @ResponseBody Result deleteDepartment(Long[] list){
		deptService.deleteTbDepartment(list);
		return new Result();
	}
	
	/**
	 * 根据部门名称查询
	 */
	@RequestMapping("/findByDepartmentName")
	public @ResponseBody Map<String,Object>findByDName(String department){
		
		return deptService.queryTbdepartmentByDName(department);
	}
	/**
	*分页显示模糊查询科室名称
	*
	**/
	@RequestMapping("/queryDnameByPage")
	public @ResponseBody Map<String,Object>queryDeptPageDname(int page,int rows,String department){
		
		
		return deptService.queryDepartmentPageDname(page, rows, department);
	}
	
	/**
	 * 根据部门地址分页查询
	 */
	
	@RequestMapping("/findByDepartmentAddressPage")
	public @ResponseBody	Map<String,Object>findByAddressPage(int page,int rows,String department){
		return deptService.queryDAddressPage(page, rows, department);
	}
	/****************************************************************************************/
	/**
	 * 分页显示员工信息包含离职
	 */
	@RequestMapping("/queryStaffByPage")
	public @ResponseBody Map<String,Object> queryStaffByPage(int page,int rows){
		return staffService.queryStaffPage(page, rows);
	}
	/**
	 * 分页显示员工信息不包含离职
	 */
	@RequestMapping("/queryStaffByPageNoOut")
	public @ResponseBody Map<String,Object> queryStaffByPageNoOut(int page,int rows){
		return staffService.queryStaffPageNoOut(page, rows);
	}
	/**
	 * 显示全部职务信息
	 * 
	 */
	@RequestMapping("/queryAllPost")
	public @ResponseBody	List<Tbpost>	queryAllPost(){
		
		return 	staffService.queryAllPost();
	}
	/**
	 * 显示全部职称信息
	 * @return
	 */
	@RequestMapping("/queryAllTitle")
	public @ResponseBody List<Tbtitle> querAllTitle(){
		return staffService.qeuryAllTitle();
	}
	/**
	 *显示全部角色信息 
	 * @return
	 */
	@RequestMapping("/queryAllRole")
	public @ResponseBody List<Tbrole> queryAllRole(){
		return staffService.queryAllRole();
	}
	/**
	 * 查询职业专属职称
	 */
	@RequestMapping("/queryAlltitlePostno")
	public @ResponseBody List<Tbtitle>queryAllTitlePostno(long postno){
		return staffService.queryAllTitlePostno(postno);
	}
	/**
	 * 添加新员工
	 * @param staff
	 * @return
	 */
	@RequestMapping("/hwc_addNewStaff")
	public @ResponseBody Result addNewStaff(Tbstaff staff){
		
		staffService.addNewStaff(staff);
		String staffinfo = "<h2>添加成功 ！<br/>姓名:"+staff.getStaffName()+
			"<br/>账号："+staff.getStaffAccount()+"</h2>"; 
		return new Result(true,staffinfo);
	}
	/**
	 * 编辑员工信息
	 * @param staff
	 * @return
	 */
	@RequestMapping("/editStaff")
	public @ResponseBody Result editStaff(Tbstaff staff){
			staffService.editStaff(staff);	
		return new Result(true,"修改成功！");
	}
	/**
	 * 根据员工编号查找员工
	 */
	
	@RequestMapping("/findStaffById")
	public @ResponseBody Tbstaff findStaffById(long staffNo){
		return staffService.findStaffById(staffNo);
	}
	
	/**
	 * 删除员工
	 */
	@RequestMapping("/deleteStaff")
	public @ResponseBody Result deleteStaff(Long[] list){
		staffService.deleteTbStaff(list);
		return new Result();
	}
	/**
	 * 员工离职
	 * @param staffNos
	 * @return
	 */
	@RequestMapping("/quitStaff")
	public @ResponseBody Result quitStaff(Long[] list){
		//System.out.println("Controller 员工离职编号"+list);
		staffService.quitStaff(list);
	
		return new Result();
	}
	
	/**
	 * 员工密码重置
	 * @param list
	 * @return
	 */
	@RequestMapping("/resetStaffpwd")
	public @ResponseBody Result resetStaffpwd(Long[] list){
		staffService.resetStaffpwd(list);
		return new Result();
		
	}
	
	/**
	 * 根据员工名查询员工（模糊查询）
	 * @param page
	 * @param rows
	 * @param staffName
	 * @return
	 */
	@RequestMapping("/findStaffNameByPage")
	public @ResponseBody Map<String,Object> findStaffNameByPage(int page,int rows,String staff){
		//System.err.println("Controller"+staff);
		return staffService.findStaffNameByPage(page, rows, staff);
	}
	
	/**
	 * 根据员工名查询员工（模糊查询）不包含离职
	 * @param page
	 * @param rows
	 * @param searche
	 * @return
	 */
	@RequestMapping("/findStaffNameNoOutByPage")
	public @ResponseBody Map<String,Object> findStaffNameNoOutByPage(int page,int rows,String searche){
		return staffService.findStaffNameNoOut(page, rows, searche);
	}
	
	/**
	 * 根据部门名称查询员工（模糊查询）不包含离职
	 * @param page
	 * @param rows
	 * @param searche
	 * @return
	 */
	@RequestMapping("/finDeptNameNoOutByPage")
	public @ResponseBody Map<String,Object>finDeptNameNoOutByPage(int page,int rows,String searche){
		return staffService.findDeptNameNoOut(page, rows, searche);
		
	}
	
	/**
	 * 根据部门名称查询该部门的员工（模糊查询）
	 * @param page
	 * @param rows
	 * @param staff
	 * @return
	 */
	@RequestMapping("/findDepartmentNameByPage")
	public @ResponseBody Map<String,Object>findDeptNameByPage(int page,int rows,String staff){
		return staffService.findDeptNameByPage(page, rows, staff);
	}
	
	/**
	 * 根据传入的员工账号查询该员工科室的其他员工
	 * @param staffAccount
	 * @return
	 */
	@RequestMapping("/findAccountByStaff")
	public @ResponseBody List<Tbstaff> findAccountByStaff(){
		return staffService.findAccountByStaff();
	}
	/**
	 * 根据传入的部门编号查询员工集合
	 * @param departmentNo
	 * @return
	 */
	@RequestMapping("/findDeptNoByStaffs")
	public @ResponseBody List<Tbstaff> findDeptNoByStaffs(long departmentNo){
		return staffService.findDeptNoByStaffs(departmentNo);
	}
	
}
